//
//  HRCommentInfoHeadView.m
//  Hear
//
//  Created by mac on 2017/10/11.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRCommentInfoHeadView.h"
#import "HRDynamicCell.h"
#import "HRCoverImageModel.h"
#import "HRCoverTimeModel.h"
#import "NSMutableAttributedString+GDAttString.h"

@interface HRCommentInfoHeadView ()
@property (nonatomic, strong)UIView *contView;

@property (nonatomic, strong)UIImageView *iconImageView;
@property (nonatomic, strong)UILabel *nickNameLabel;
@property (nonatomic, strong)UILabel *timeLabel;
@property (nonatomic, strong)UILabel *contentLabel;
@property (nonatomic, strong)HRSoundButton *soundButton;
@property(nonatomic, copy)NSString *commentText;

@end
@implementation HRCommentInfoHeadView


-(void)setCommentModel:(HRDynamicCommentModel *)commentModel{
    
    WeakSelf
    if (commentModel.CommentUserPictureBackgroundColor.length){
        [HRCoverImageModel OriginImage:[UIImage imageNamed:commentModel.CommentUserPictureName] scaleToSize:CGSizeMake(75, 75) callBackOriginImage:^(UIImage *originImage) {
            weakSelf.iconImageView.image = originImage;
        }];
        self.iconImageView.backgroundColor = [UIColor colorWithHexString:commentModel.CommentUserPictureBackgroundColor];
    }else{
        [self.iconImageView.layer setImageWithURL:[NSURL URLWithString:commentModel.CommentUserPictureName] placeholder:[UIImage imageNamed:@""]];
    }
    

    self.timeLabel.text = [HRCoverTimeModel covetTime:commentModel.CreateTime];

    NSMutableAttributedString *attNickname = [NSMutableAttributedString attStringFromTextForChatting:commentModel.UserRemark.length>0?commentModel.UserRemark:commentModel.CommentUserNickName textFont:CustomUIFont(13)];
    
    [attNickname setColor:SecondTextColor range:NSMakeRange(0, (commentModel.UserRemark.length>0?commentModel.UserRemark:commentModel.CommentUserNickName).length)];
    
    
    self.nickNameLabel.attributedText = attNickname;
    

    
    self.contentLabel.attributedText = [NSMutableAttributedString arrtLabelText:commentModel.CommentText arrFont:CustomUIFont(14) attColor:CustomColor(66, 66, 66, 1) lineLengh:3 alignment:0 colorLenghFrom:0 colorLengh:commentModel.CommentText.length];
    
    [self updataFrame];

}

- (void)updataFrame{
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = 3;
    NSDictionary *dic = @{NSFontAttributeName:CustomUIFont(14), NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@0.0f};
    
    _contentLabel.height = [_contentLabel.text boundingRectWithSize:CGSizeMake(_contentLabel.width, HUGE) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.height;

    self.contView.height = _contentLabel.bottom+15;
    
    self.headHeight = self.contView.height+5;
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = CustomColor(240, 240, 240, 1);
        WeakSelf
        CGFloat leftPading = 15;

        UIView *contView = [UIView new];
        [self addSubview:(self.contView = contView)];
        contView.backgroundColor = [UIColor whiteColor];
        contView.userInteractionEnabled = YES;
        self.contView.frame = CGRectMake(0, 0, kFullWidth, 40);


        UIImageView *iconImageView = [UIImageView new];
        iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        iconImageView.clipsToBounds = YES;
        iconImageView.layer.cornerRadius = 25;
        [contView addSubview:(self.iconImageView = iconImageView)];
        iconImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *iconTap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            if (weakSelf.clickIconBock){
                weakSelf.clickIconBock();
            }
        }];
        [iconImageView addGestureRecognizer:iconTap];
        iconImageView.frame = CGRectMake(leftPading, leftPading, 50, 50);
        
        
        self.nickNameLabel = [UILabel new];
        self.nickNameLabel.textColor = CustomColor(28, 190, 160, 1);
        self.nickNameLabel.font = CustomUIFont(16);
        [contView addSubview:self.nickNameLabel];
        self.nickNameLabel.frame = CGRectMake(iconImageView.right+leftPading, iconImageView.top+5, kFullWidth-leftPading*3-iconImageView.width, 25);
        
        self.timeLabel = [UILabel new];
        self.timeLabel.font = CustomUIFont(12);
        self.timeLabel.textColor = CustomColor(144, 144, 144, 1);
        [contView addSubview:self.timeLabel];
        self.timeLabel.frame = CGRectMake(_nickNameLabel.left, _nickNameLabel.bottom, _nickNameLabel.width, 10);
        
        self.contentLabel = [UILabel new];
        self.contentLabel.textColor = CustomColor(66, 66, 66, 1);
        self.contentLabel.font = CustomUIFont(14);
        self.contentLabel.numberOfLines = 0;
        [contView addSubview:self.contentLabel];
        self.contentLabel.userInteractionEnabled = YES;
        [self.contentLabel addGestureRecognizer: [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)]];
        self.contentLabel.frame =  CGRectMake(self.nickNameLabel.left, self.iconImageView.bottom+leftPading, kFullWidth-leftPading-self.nickNameLabel.left, 20);

        

        
        _soundButton = [HRSoundButton buttonWithType:0];
        [contView addSubview:_soundButton];
        _soundButton.playMedioBlock = ^(HRDynamicModel *currentDynamicModel){
//            if (weakSelf.playMedioBlock){
//                weakSelf.playMedioBlock(currentDynamicModel);
//            }
        };

    }
    return self;
}


@end
